/*
 *  Copyright (c) 2013 The CCP project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a Beijing Speedtong Information Technology Co.,Ltd license
 *  that can be found in the LICENSE file in the root of the web site.
 *
 *   http://www.yuntongxun.com
 *
 *  An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */
package com.huaxindata.im.main.chat.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.huaxindata.im.R;
import com.huaxindata.im.model.emoji.HxEmotion;

import java.util.List;


/**
 * <p>Title: EmojiAdapter.java</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2014</p>
 * <p>Company: Beijing Speedtong Information Technology Co.,Ltd</p>
 * @author Jorstin Chan@容联•云通讯
 * @date 2014-12-10
 * @version 4.0
 */
public class EmojiAdapter extends BaseAdapter {


	List<HxEmotion> emojis;
	
	LayoutInflater mInflater;

    public EmojiAdapter(Context context, List<HxEmotion> emojis) {
    	mInflater = LayoutInflater.from(context);
		this.emojis=emojis;
    }


	@Override
	public int getCount() {

		if(emojis != null && emojis.size() > 0) {
			return this.emojis.size() + 1;//加1是因为在最后添加一个删除按钮
		}
		return 0;
	}


	@Override
	public Object getItem(int position) {
		
		if(emojis != null && (position <= (emojis.size() - 1)))  {
			return emojis.get(position);
		}
		return null;
	}


	@Override
	public long getItemId(int position) {
		return position;
	}
	

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if(convertView == null || convertView.getTag() == null ) {
            viewHolder=new ViewHolder();
            convertView=mInflater.inflate(R.layout.row_expression, parent,false);
            viewHolder.emojiTv =(TextView)convertView.findViewById(R.id.expression_tv);
            viewHolder.emojiIv =(ImageView) convertView.findViewById(R.id.expression_iv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder=(ViewHolder)convertView.getTag();
        }
        if(getCount() - 1 == position) {
			viewHolder.emojiTv.setVisibility(View.GONE);
			viewHolder.emojiIv.setVisibility(View.VISIBLE);
    		viewHolder.emojiIv.setImageResource(R.drawable.emoji_del_selector);
        } else {
			viewHolder.emojiIv.setVisibility(View.GONE);
			viewHolder.emojiTv.setVisibility(View.VISIBLE);
        	HxEmotion emoji=(HxEmotion) getItem(position);
			viewHolder.emojiTv.setText(emoji.getEmojiName());
        }

        return convertView;
    }

    class ViewHolder {

        public TextView emojiTv;
		public ImageView emojiIv;
    }
    
    public void release() {
    	if(emojis != null) {
    		emojis.clear();
    		emojis = null;
    	}
    	mInflater = null;
    }

}